package com.jjh.config;

import com.jjh.interceptor.TokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
@EnableWebMvc
@Slf4j
public class WebConfig implements WebMvcConfigurer  {

    @Autowired
    private TokenInterceptor tokenInterceptor;

//    @Bean
//    public CorsFilter corsFilter(){
//        CorsConfiguration config = new CorsConfiguration();
//        config.addAllowedOrigin("*");
//        config.addAllowedMethod("*");
//        config.addAllowedHeader("*");
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**",config);
//        return new C
//    }




        @Override
        public void addCorsMappings(CorsRegistry registry) {

            registry.addMapping("/**")
                    .allowedOrigins("*") // 允许所有源
                    .allowedMethods("*") // 允许所有HTTP方法
                    .allowedHeaders("*") // 允许所有请求头
                   // .allowCredentials(true) // 允许带上cookies
                    // 暴露哪些头部信息
                    .exposedHeaders("*")
                    //时间
                    .maxAge(18000);
        }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(List.of("/api/login","/api/register","/api/checkcode","/elm"));
    }
}
